@include('public.page_styles')
  <link rel="stylesheet" href="{{ asset("/AdminLTE/bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css") }}">
<style>
    .couponStyle{
        padding: 2px 12px;
        background-color: #e4a33a;
    }
</style>

</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
  @include('public.top')
  <!-- Left side column. contains the logo and sidebar -->
  @include('public.sidebar')
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper" id="w_container" >
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
          {{ trans('message.sendCoupon') }}
        <small>{{ trans('message.list') }}</small>
      </h1>
      <ol class="breadcrumb">
        <li><a ><i class="fa fa-dashboard"></i> {{ trans('message.home') }}</a></li>
        <li><a href="{{ url('userAccount/activeManage/list') }}" >{{ trans('message.activeUser') }}</a></li>
        <li class="active">{{ trans('message.sendCoupon') }}</li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
      <div class="row">
        <div class="col-xs-12">
          <div class="box">
              <div class="box-header with-border">
                  {{--<h3 class="box-title"> {{ trans('message.add') }}</h3>--}}
                  <div>
                      <div style="margin-bottom:5px;">你将给以下用户发放优惠券：</div>
                      <div  style="border-top: 2px solid #999fa5;padding-top: 5px;width: 80%;">
                          <div style="word-wrap:break-word;font-size:20px;font-family: initial;" id="phone">{{ $user_phones }}</div>
                          <span style="display: none" id="open_id">{{ $user_openIds }}</span>
                      </div>
                  </div>
                  <div class="box-tools">
                      <div class="btn-group pull-right" style="margin-right: 10px">
                          <a href="{{ url('userAccount/activeManage/list') }}" class="btn btn-sm btn-default"><i class="fa fa-list"></i>&nbsp; {{ trans('message.list') }}</a>
                      </div>
                      <div class="btn-group pull-right" style="margin-right: 10px">
                          <a class="btn btn-sm btn-default form-history-back" onclick="goBack()" ><i class="fa fa-arrow-left"></i>&nbsp;{{ trans('message.history-back') }}</a>
                      </div>
                  </div>
              </div>
            <!-- /.box-header -->
            <div class="box-body">
              <table id="example1" class="table table-bordered table-striped">
                <thead>
                <tr>
                    <th style="width: 50px"><input type="checkbox" id="checkAll" name="checkAll" />全选</th>
                    <th>所属网点</th>
                    <th>优惠券名称</th>
                    <th>金额</th>
                    <th>数量</th>
                    <th>有效期（有效区间或者月份）</th>
                </tr>
                </thead>
                <tbody>
                @if($coupon)
                    @foreach($coupon as $cp)
                        <tr>
                            <td style="text-align: center;"><input type="checkbox" name="checkItem" value="{{ $cp->id }}"></td>
                            <td>{{ $cp->bank_show_name }}</td>
                            <td>{{ $cp->name }}</td>
                            <td>{{ $cp->money }}</td>
                            <td>
                                <a class="icon-minus" style="cursor: pointer;"  onclick="reduce('{{ $cp->id }}')"><img src="{{ asset('/image/icon/reduce.png') }}"></a>　
                                <span  id="num_{{ $cp->id }}" >1</span>　
                                <a class="icon-plus" style="cursor: pointer;"  onclick="plus('{{ $cp->id }}')"><img src="{{ asset('/image/icon/plus.png') }}"></a>
                            </td>
                            <td id="seTime_{{ $cp->id }}">
                                @if($cp->startTime)
                                    {{ $cp->startTime }} ~ {{ $cp->endTime }}
                                @else
                                    {{ $cp->validity }}
                                    {{--@if($cp->validity == 0)--}}
                                        {{--无限期--}}
                                    {{--@else--}}
                                        {{--领取之日 {{ $cp->validity }}个月内--}}
                                    {{--@endif--}}
                                @endif
                            </td>
                        </tr>
                    @endforeach
                @endif
                </tbody>
              </table>
                <div>
                    <button class="btn" id="sendAll">发送</button>
                </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  </div>
  <!-- /.content-wrapper -->
  @include('public.bottom')

  <!-- Control Sidebar -->
  @include('public.aside')
  <!-- /.control-sidebar -->
  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->
@include('public.javascript')
<script src="{{ asset ("/AdminLTE/bower_components/datatables.net/js/jquery.dataTables.min.js") }}" type="text/javascript"></script>
<script src="{{ asset ("/AdminLTE/bower_components/datatables.net-bs/js/dataTables.bootstrap.min.js") }}" type="text/javascript"></script>
<script src="{{ asset ("/AdminLTE/bower_components/jquery-slimscroll/jquery.slimscroll.min.js") }}" type="text/javascript"></script>
{{--<script src="{{ asset ("/AdminLTE/bower_components/fastclick/lib/fastclick.js") }}" type="text/javascript"></script>--}}
<script src="{{ asset ("/js/bootbox.js") }}" type="text/javascript"></script>

<!-- page script -->
<script>
    $(function(){
      $('#example1').DataTable({
          "language": {
              "url":"{{ asset ("/AdminLTE/bower_components/datatables.net/chinese.json") }}"
          },
          "aaSorting": [[ 4, "desc" ]],//默认第几个排序
          "bStateSave": true,//状态保存
          "aoColumnDefs": [
              {"orderable":false,"aTargets":[0,1,2,3]}// 制定列不参与排序
          ]
      });

      @if(session()->has('success'))
           alertMess("{{session()->get('success')}}");
      @endif

      $('#sendAll').click(function(){
          var ids = '';
          var nums = ''; // 每张券发送的数量
          var seTime = ''; //  优惠券有效期（开始/结束时间）

          $('input:checkbox[name=checkItem]:checked').each(function(i){
              if(0==i){
                  ids = $(this).val();
                  nums = $('#num_'+$(this).val()).html();
                  seTime = $('#seTime_'+$(this).val()).html();
              }else{
                  ids += (","+$(this).val());
                  nums +=(","+ $('#num_'+$(this).val()).html());
                  seTime +=(","+  $('#seTime_'+$(this).val()).html());

              }
          });

          console.log(ids);
          if(ids.length==0){
              alert('请选择要发送的优惠券');
              return;
          }

          var phone = $('#phone').html();
          var open_id = $('#open_id').html();

          // 发送优惠券-----------------
          $.ajax({
              url: "{{ url('userAccount/activeManage/sendSave') }}",
              type: "get",
              data: {
                  coupon_id:ids, //  优惠券id
                  phone:phone,
                  open_id:open_id,
                  nums:nums,
                  seTime:seTime
              },
              success: function (response){
//                  console.log(response);
                  if(response== 1){
                      alertM('发送成功'); // 跳转
                  }else{
                      alertMess('发送失败'); // 本页刷新
                  }
              }
          });
      });

  });

  $(function(){
      var ids = '';
      function initTableCheckbox() {
          var $thr = $('table thead tr');

          $('#checkAll').click(function(event){
              $tbr.find('input').prop('checked',$(this).prop('checked'));
              if ($(this).prop('checked')) {
                  $tbr.find('input').parent().parent().addClass('warning');
              } else{
                  $tbr.find('input').parent().parent().removeClass('warning');
              }
              /*阻止向上冒泡，以防再次触发点击操作*/
              event.stopPropagation();
          });


          /*点击全选框所在单元格时也触发全选框的点击操作*/
          $('#checkAll').click(function(){
              $(this).find('input').click();
          });

          var $tbr = $('table tbody tr');
          $tbr.find('input').click(function(event){

              $(this).parent().parent().toggleClass('warning');
              $('#checkAll').prop('checked',$tbr.find('input:checked').length == $tbr.length ? true : false);
              /*阻止向上冒泡，以防再次触发点击操作*/
              event.stopPropagation();

          });
      }
      initTableCheckbox();
  });

    function reduce(id){
        var num = parseInt($('#num_'+id).html());
        if(num==1){
            alert('最小值是1');
            return;
        }
        $('#num_'+id).html(num-1);
    }
    function plus(id){
        var num = parseInt($('#num_'+id).html());
        $('#num_'+id).html(num+1)
    }

    function alertM($mess) {
        toastr.success($mess);
        setTimeout(function () {
            window.location.href = "{{ url('userAccount/activeManage/list') }}";
        }, 2000);
    }

</script>
</body>
</html>
